package com.zhihesj.anan.system.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhihesj.anan.message.Message;
import com.zhihesj.anan.message.MessageCodeEnum;
import com.zhihesj.anan.system.entity.Menu;
import com.zhihesj.anan.system.service.MenuService;
import com.zhihesj.anan.system.vo.MenuVo;
import com.zhihesj.anan.utils.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author anan
 * @since 2020-07-22
 */
@RestController
@RequestMapping("/system/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @GetMapping("/getMenuList")
    public Message getMenuList(MenuVo menuVo){
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menuVo, false, true);
        List<Menu> menus=menuService.list();
        BaseUtil baseUtil =new BaseUtil();
        if (stringObjectMap.isEmpty() || stringObjectMap.size()==0){
            List<MenuVo> list = baseUtil.ArrangementMenuTree(menus, 0L);
            return Message.success(list);
        }
        List<Menu> iPageMenu=menuService.selectPage(menuVo);
        List<MenuVo> menuVoList= baseUtil.ArrangementMenuTree(iPageMenu,menus);
        return Message.success(menuVoList);
    }

    @RequestMapping("/getMenuListMC")
    public Message getMenuListMC(MenuVo menuVo){
        BaseUtil baseUtil =new BaseUtil();

        // 获取除按钮外的全部菜单
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("menu_type", "F");
        List<Menu> menus=menuService.list(queryWrapper);

        // 查询父菜单
        List<Menu> iPageMenu=menuService.selectPage(menuVo);
        List<MenuVo> menuVoList= baseUtil.ArrangementMenuTree(iPageMenu,menus);
        return Message.success(menuVoList);
    }

    @RequestMapping("/getMenuLists")
    public Message getMenuLists(MenuVo menuVo){
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menuVo, false, true);

//        获取除按钮外的全部菜单
        QueryWrapper<Menu> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.ne("menu_type", "F");
        List<Menu> menus=menuService.list(queryWrapper1);

        BaseUtil baseUtil =new BaseUtil();
        if (stringObjectMap.isEmpty()){
            return Message.success(menus);
        }
        if (menuVo.getParentId()==null){
            List<MenuVo> list = baseUtil.ArrangementMenuTree(menus, 0L);
            return Message.success(list);
        }
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", menuVo.getParentId());
        List<Menu> iPageMenu=menuService.selectPage(menuVo);
        List<MenuVo> menuVoList= baseUtil.ArrangementMenuTree(iPageMenu,menus);
        return Message.success(menuVoList);
    }

    @PostMapping("/createMenu")
    public Message createMenu(@RequestBody Menu menu){
        if (!menu.toString().isEmpty()) {
            menuService.save(menu);
        }
        return Message.success(MessageCodeEnum.SUCCESS);
    }

    @PostMapping("/updateMenuById")
    public Message updateMenuById(@RequestBody Menu menu){
        if (menuService.removeById(menu.getMenuId())) {
            menuService.save(menu);
        }
        return Message.success(MessageCodeEnum.SUCCESS);
    }

    @DeleteMapping("/delMenuById")
    public Message delMenuById(@RequestParam String menuId){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("menu_id", menuId);
        menuService.remove(wrapper);
        return Message.success(MessageCodeEnum.SUCCESS);
    }

}
